package com.lr.transfer.config;

import org.springframework.stereotype.Component;

import javax.servlet.ServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 拦截器的公共基础方法
 */
@Component
public class HttpHandle {

    /**
     * 构建json返回
     * @param msg
     * @param res
     * @return
     */
    public  String jsonContent(String msg, ServletResponse res) {
        res.setContentType("text/html; charset=utf-8");
        try {
            PrintWriter out = res.getWriter();
            out.print(msg);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取json请求
     * @return
     */
    public String readJSONString(BufferedReader br){
        StringBuffer json = new StringBuffer();
        String line = null;
        try {
            while((line = br.readLine()) != null) {
                json.append(line);
            }
        }
        catch(Exception e) {
            System.out.println(e.toString());
        }
        return json.toString();
    }

}
